package com.myplaylist.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.List;


import com.myplaylist.model.Track;
import com.myplaylist.utils.PlaylistXMLParser;

public class TcpConnectionHandler extends Thread {

	private Socket socket;

	public TcpConnectionHandler(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			System.out.println("created br");
			String buff = "";
			
			StringBuffer sb = new StringBuffer();
			
			while ((buff = br.readLine()) != null) {
				sb.append(buff);
			}
			
			System.out.println("exited while");
			
			System.out.println("Result is: " + sb.toString());
			
			File xmlFile = new File("tmp.xml");
			
			PrintWriter pw = new PrintWriter(xmlFile);
			pw.write(sb.toString());
			pw.close();
			
			PlaylistXMLParser playlistXMLParser = new PlaylistXMLParser();
			List<Track> list = playlistXMLParser.processTrackList(xmlFile);
			System.out.println("List size: " + list.size());
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
}
